// pages/defect/adjustment/index.js
import {
  _list,
  _add,
  _update,
  _remove
} from '../../api/adjustment-glaze'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [ ]
  },
  goto() {
    wx.navigateTo({
      url: './main/index',
    })
  },
  onEdit(event){
       const {
         index,
         item
       } = event.currentTarget.dataset;
       const dataSource = this.data.dataSource;
       wx.showModal({
        editable: true,
        title: '釉料名称',
        placeholderText: '输入',
        content: item.name,
        success: res => {
          if (res.confirm) {
            if (res.content) {
              _update({
                ...item,
                name: res.content 
              }, ()=>{
                dataSource[index] = {
                  ...item,
                  name: res.content
                }
                this.setData({
                  dataSource
                })
              })
            } else {
              wx.showToast({
                title: '釉料名称无效',
                icon: 'none'
              })
            }
          }
        }
      })
  },
  onAdd() {
    wx.showModal({
      editable: true,
      title: '釉料名称',
      placeholderText: '输入',
      success: res => {
        if (res.confirm) {
          if (res.content) {
            _add({
              name: res.content,
              useType: "WORKMANSHIP_PARAM"
            }, this.fetchData)
          } else {
            wx.showToast({
              title: '釉料名称无效',
              icon: 'none'
            })
          }
        }
      }
    })
  },
  onDelect(event) {
    const dataSource = this.data.dataSource,
      index = event.currentTarget.dataset.index;
    wx.showModal({
      title: '确定要删除吗？',
      icon: 'none',
      success: res => {
        if (res.confirm) {
          _remove(dataSource[index], ()=>{
            dataSource.splice(index, 1);
            this.setData({
              dataSource
            })
          })
        }
      }
    })
  },
  fetchData() {
    _list({}, res => {
       this.setData({
         dataSource: res
       })
    })
  },
  toCraft(event){
    const item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: './craft/index?data='+ encodeURIComponent(JSON.stringify(item)),
    })
  },
  
  
  toFormula(event){
    const item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: './formula/index?data='+ encodeURIComponent(JSON.stringify(item)),
    })
  },
  toAdjustment(event) {
    
    const item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: './adjustment/index?data='+ encodeURIComponent(JSON.stringify(item)),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchData()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})